package com.car.controller.custormer;

import com.car.entity.Custormer;
import com.car.service.custormer.CustormerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: CustormerController
 * @Description:
 * @Author 余全盛
 * @Date: 2021/12/3 15:35
 */
@Controller
public class CustormerController {
    @Autowired
    private CustormerService custormerService;

    @RequestMapping("/toCustomerInfo")
    public ModelAndView toCutsomerInfo(@RequestParam int customerId) {
        ModelAndView mv = new ModelAndView("customer-info");
        Custormer customer = custormerService.getCustormerById(customerId);
        System.out.println(customer);
        mv.addObject("customer", customer);
        return mv;
    }

    @RequestMapping("/getCustormerById")
    @ResponseBody
    public String getCustormerById(int custormerId) {
        Custormer custormer = custormerService.getCustormerById(custormerId);
        return custormer == null ? "0" : "1";
    }

    @RequestMapping("/regCustomer")
    @ResponseBody
    public String regCustomer(Custormer custormer, HttpSession session) {
        int id = custormerService.regCustormer(custormer);
        String strId = custormer.getCustormerId().toString();
        session.setAttribute("strId",strId);
        return id>=1? "创建成功" : "登记失败";
    }
}
